package MontCarl;
import java.io.IOException;
import java.lang.Math.*;
import java.util.Random;
import java.util.Random.*;
import java.util.concurrent.ExecutionException;

import Abstract.AbstractMC;


public class MonteCarloSimulation extends AbstractMC {
	
	public int choice;
	
	public double computeMonteCarlo(String CallPutFlag, double S ,double X,double T ,  
			  double r ,double b,double v,  
			  int nSteps ,int nSimulations){
		double result= 0.0;
		double dt, St, Sum, Drift,vSqrdt;
		int i, j, z;
		Sum = 0.0;
		Random rand = new Random();
		z = 0;
		
		dt = T/nSteps ;
		Drift= (b - (v*v) / 2 ) * dt;
		vSqrdt = v * Math.sqrt(dt);
		if(CallPutFlag == "c") z = 1;
		else if (CallPutFlag == "p") z=-1;
		for (i=0; i<nSimulations; i++){
			St = S;
			for (j=0; j< nSteps; j++){
				St = St * Math.exp(Drift + vSqrdt * rand.nextGaussian());
			}
			Sum = Sum + Math.max(z*(St - X), 0.0);			
		}
		result = Math.exp(-r * T) * (Sum/nSimulations);
		return result;
	}
	
	 public static void main(final String[] args) throws ExecutionException, InterruptedException, IOException {
		 new MonteCarloSimulation().timeAndComputeValue();
	 }

}
